/* 导入全局事件的模块 */
import mitt from 'stores/mitt/index.js'
/* 查看和添加公告组件的显示隐藏 */
import { viewPublish } from 'components/Announcements/Notice/hooks/viewNotice'
/* 创建画笔 */
import { createDraw } from './draw'
import { ref } from 'vue';
/* 查询工具 */
import { Query } from './ES6Query.js'
/* 画笔 */
const draw = ref(null)
/* 矢量数据源 */
let source
/* 结果图层 */
const resultLayer = ref(null)
/* 画布图层 */
const addAounceLayer = ref(null)
export class addAounce {
    // 1, 发布公告事件回调函数
    static announce(map, attrs) {
        // 1.1创建画布
        /* 画布的矢量数据源 */
        source = new ol.source.Vector({
            wrapx: false,
            crossOrigin: 'Anonymous',
        })
        /* 画布的矢量图层 */
        addAounceLayer.value = new ol.layer.Vector({
            source: source,
        })
        draw.value = createDraw({
            source,
            type: 'LineString',
            callback: e => addAounce.lineSuccess(e, map, attrs)
        })
        map.addInteraction(draw.value)
        map.addLayer(addAounceLayer.value)
    }
    // 2, 添加画笔的成功的回调--执行缓冲区分析
    static lineSuccess(e, map, attrs) {
        //清除画笔
        setTimeout(() => {
            map.removeInteraction(draw.value)
        }, 100);
        console.log('开始执行缓冲区分析');
        //获取点数据
        const coordinates = e.target.sketchCoords_
        //进行缓冲区分析
        const pointObj = new Array()
        //二维数组需要遍历,再添加到对象obj中
        coordinates.forEach((item) => {
            pointObj.push(new Zondy.Object.Point2D(item[0], item[1]))
        })
        //构成折线的弧段
        const gArc = new Zondy.Object.Arc(pointObj)
        //构成线的折线
        const gAnyLine = new Zondy.Object.AnyLine([gArc])
        //设置线要素的几何信息
        const gline = new Zondy.Object.GLine(gAnyLine)
        //设置要素的几何信息
        const fGeom = new Zondy.Object.FeatureGeometry({ LinGeom: [gline] })
        //设置属性结构
        const regAttStr = new Zondy.Object.CAttStruct({
            FldName: ['ID', '面积', '周长', 'LayerID'],
            FldNumber: 4,
            FldType: ['FldLong', 'FldDouble', 'FldDouble', 'FldLong'],
        })
        //实例化CAttDataRow类
        var values = [0, 62.566714, 50.803211, 0]
        var valuesRow = new Zondy.Object.CAttDataRow(values, 1)
        //实例化FeatureBuffBySingleRing类，设置要素缓冲分析必要参数，输出分析结果到缓冲分析结果图层
        var featureBufBySR = new Zondy.Service.FeatureBuffBySingleRing({
            ip: 'localhost',
            port: '6163',
            //设置要素缓冲分析左半径
            leftRad: 0.002,
            //设置要素缓冲分析右半径
            rightRad: 0.002,
        })
        /*设置缓冲分析参数*/
        //设置几何信息
        featureBufBySR.sfGeometryXML = JSON.stringify([fGeom])
        //设置属性结构
        featureBufBySR.attStrctXML = JSON.stringify(regAttStr)
        //设置属性值
        featureBufBySR.attRowsXML = JSON.stringify([valuesRow])
        //设置追踪半径
        featureBufBySR.traceRadius = 0.0001
        //设置缓冲结果的名称以及存放地址
        featureBufBySR.resultName = 'gdbp://MapGisLocal/wuhan/ds/buffer/sfcls/bufferresult' + Date.now()
        //调用Zondy.Service.AnalysisBase基类的execute方法执行要素缓冲分析，AnalysisSuccess为回调函数。
        featureBufBySR.execute(result => addAounce.bufferSuccess(result, map, attrs), 'post', false, 'json', () => {
            console.log("缓冲区创建失败");
        })
    }
    /**
     * 3.缓冲区分析成功回调函数，执行裁剪分析
     * @param {object} result 缓冲区分析结果对象
     */

    static bufferSuccess(result, map, attrs) {
        console.log("缓冲区创建成功");
        console.log(result.results[0].Value);
        /* 路径 */
        var clspath = result.results[0].Value
        //实例化ClipByLayer类
        var clipParam = new Zondy.Service.ClipByLayer({
            ip: 'localhost',
            port: '6163',
            //源简单要素类的URL
            srcInfo1: 'gdbp://MapGisLocal/wuhan/sfcls/居民区',
            //裁剪框简单要素类的URL
            srcInfo2: clspath,
            //设置结果URL
            desInfo:
                'gdbp://MapGisLocal/wuhan/ds/clip/sfcls/clipresult' + Date.now(),
            infoOptType: 0,
        })
        //调用基类的execute方法，执行图层裁剪分析。AnalysisSuccess为结果回调函数
        clipParam.execute(res => addAounce.clipSuccess(res, map, attrs), 'post', false, 'json', () => {
            console.log("缓冲区分析失败");
        })
    }
    //4. 缓冲区裁剪成功,添加到图层
    static clipSuccess(result, map, attrs) {
        const resultName = result.results[0].Value
        if (result.succeed) {
            console.log('缓冲区分析成功!');
            resultLayer.value = new Zondy.Map.GdbpLayer('', [resultName], {
                ip: 'localhost',
                port: 6163,
            })
            /* 2、设置要素的样式信息 */
            /* 2、设置要素的样式信息 */
            /*            var style = new ol.style.Style({
                           image: new ol.style.Circle({
                               radius: 20,
                               fill: new ol.style.Fill({
                                   color: '#ffcc33'
                               })
                           })
                       }) */
            /* resultLayer.value.setStyle(style) */
            map.addLayer(resultLayer.value)
        }
        //查询所裁剪图层的属性
        Query.queryByLayerUrl({
            url: resultName,
            callback: res => addAounce.querySuccess(res, map, attrs),
        })
    }
    //4.查询成功把查询结果添加到数组
    static querySuccess(result, map, attrs) {
        //数组遍历获取对应的名称和电话
        result.SFEleArray.forEach((item) => {
            attrs.push({ name: item.AttValue[4], tel: item.AttValue[5] })
        })
        //2s后显示弹窗添加信息
        setTimeout(() => {
            /* 清除source */
            source.clear()
            /* 清除画笔图层 */
            map.removeLayer(addAounceLayer.value)
            /* 清除结果图层 */
            map.removeLayer(resultLayer.value)
            /* 使用全局模块传值 */
            console.log("使用全局模块传值");
            mitt.emit('communityInfo', attrs)
            viewPublish()
        }, 2000)
    }
}